﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.soft
{
      public partial class softcomment_e : System.Web.UI.Page
      {
            private readonly D_T_SoftWare m_soft = new D_T_SoftWare();
            private readonly D_T_SoftWareEvaluating_Exp m_soft_comment = new D_T_SoftWareEvaluating_Exp();

            protected void Page_Load(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {

                        if (!IsPostBack)
                        {
                              BindDropDown(this.ddlSoftList, m_soft.GetList(string.Empty, null));
                              //存储主键
                              var id = Request.Params["ID"].Exp_IntTryParse();
                              ViewState.Add("ID", id);
                              if (id > 0)
                              {
                                    var model = m_soft_comment.GetModel(id);
                                    if (model == null)
                                    {
                                          this.Exp_PageSkyAlterReplace("softcomment.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                                    }
                                    else
                                    {
                                          SetControlsData(model);
                                    }
                              }
                        }
                        //工具栏初始化
                        WebUserToolBar1.EditInit(
                            UserConst.Exp_GetUser,
                            UserConst.Exp_GetButtonList,
                            SetBut,
                            UserConst.Exp_ToolBarErr);
                  });
            }


            #region 固定方法
            /// <summary>
            ///  设置按钮资料
            /// </summary>
            /// <param name="i"></param>
            /// <param name="but"></param>
            private void SetBut(int i, Button but)
            {
                  AdminPublic.Try(() =>
                  {
                        switch (i)
                        {
                              case 1:
                                    but.CausesValidation = false;
                                    break;
                              default:
                                    break;
                        }
                  });

            }

            /// <summary>
            /// 编辑模式的时候,填充窗口
            /// </summary>
            /// <param name="model"></param>
            private void SetControlsData(M_T_SoftWareEvaluating model)
            {
                  AdminPublic.Try(() =>
                  {
                        this.rbtTopIS.SelectedValue = model.Topis.Exp_Trim();
                        this.FCKeditor1.Value = model.Contents.Exp_Trim();
                        this.ddlSoftList.SelectedValue = model.SoftWareID.Exp_Trim();
                        this.txtExteriorKey.Text = model.ExteriorKey.Exp_Trim();
                        this.txtWebTitle.Text = model.WebTitle.Exp_Trim();
                        this.txtWebSummary.Text = model.WebSummary.Exp_Trim();
                        this.txtSource.Text = model.Source.Exp_Trim();
                        this.txtTitle.Text = model.Title.Exp_Trim();
                        this.rbtRem.SelectedValue = model.RemSign ? "1" : "0";
                  });
            }

            /// <summary>
            /// 从页面控件中构造一个实体model
            /// </summary>
            /// <returns></returns>
            private M_T_SoftWareEvaluating ControlsToModel()
            {
                  var model = new M_T_SoftWareEvaluating()
                  {
                        Flag = true,
                        id = ViewState["ID"].Exp_IntTryParse(),
                        Title = this.txtTitle.Text.Exp_Trim(),
                        ColorKey = string.Empty,
                        ColorTitle = string.Empty,
                        Contents = this.FCKeditor1.Value.Exp_Trim(),
                        Source = this.txtSource.Text.Exp_Trim(),
                        UploadDate = DateTime.Now,
                        Topis = this.rbtTopIS.SelectedValue.Exp_IntTryParse(),
                        Pic = string.Empty,
                        SoftWareID = this.ddlSoftList.SelectedValue.Exp_IntTryParse(),
                        InteriorKey = string.Empty,
                        ExteriorKey = this.txtExteriorKey.Text.Exp_Trim(),
                        RemSign = this.rbtRem.SelectedValue == "1" ? true : false,
                        WebKey = this.txtExteriorKey.Text.Exp_Trim(),
                        WebTitle = this.txtTitle.Text.Exp_Trim(),
                        WebSummary = this.txtWebSummary.Text.Exp_Trim(),
                        UserID = UserConst.Exp_GetUser(this).id
                  };
                  return model;
            }
            /// <summary>
            /// 操作完成后,处理各种提示用
            /// </summary>
            /// <param name="iRef"></param>
            /// <param name="LogType"></param>
            public void EditSaveMsg(int iRef, Log_Type LogType)
            {
                  if (iRef > 0)
                  {
                        this.Exp_PageWriteLog(LogType, "软件评测资料维护", iRef);
                        this.Exp_PageSkyAlterReplace("softcomment.aspx", WebUserToolBar1.EditSKey, "操作成功");
                  }
                  else
                  {
                        switch (iRef)
                        {
                              default:
                                    this.Exp_PageAlter("操作失败");
                                    break;
                        }
                  }
            }
            #endregion


            private void BindDropDown(DropDownList ddlSoftList, List<M_T_SoftWare> lstSoft)
            {
                  if (ddlSoftList != null)
                  {
                        ddlSoftList.Items.Clear();
                        foreach (var item in lstSoft)
                        {
                              ddlSoftList.Items.Add(new ListItem
                              {
                                    Value = item.id.Exp_Trim(),
                                    Text = item.Title.Exp_Trim()
                              });
                        }

                        if (Request.QueryString["t"] == "soft" && Request.QueryString["softid"] != "")
                        {
                              this.ddlSoftList.SelectedValue = Request.QueryString["softid"].Exp_IntTryParse().Exp_Trim();
                              this.ddlSoftList.Enabled = false;
                        }
                  }
            }

            protected void WebUserToolBar1_Button0(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        //注意,统一,先获取实体,然后根据实体进行各种判断
                        var model = ControlsToModel();

                        var LogType = Log_Type.add;

                        var iRef = 0;
                        if (0 >= model.id)
                        {
                              iRef = m_soft_comment.Add(model);
                        }
                        else
                        {
                              //    修改的专用判断
                              LogType = Log_Type.update;
                              var Temp = m_soft_comment.ExistsFlag(model.id);
                              if (!Temp)
                              {
                                    this.Exp_PageAlter("资料不存在,可能被其他人删除");
                                    return;
                              }

                              iRef = m_soft_comment.Update(model);
                        }
                        EditSaveMsg(iRef, LogType);
                  });
            }

            protected void WebUserToolBar1_Button1(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        if (Request.QueryString["t"] == "soft" && Request.QueryString["softid"] != "")
                        {
                              var key = Request.QueryString["Skey"];
                              Response.Redirect("softlist.aspx?Skey=" + Request.QueryString["Skey"]);
                        }
                        else 
                        {
                              var sKey = WebUserToolBar1.EditSKey;
                              Response.Redirect("softcomment.aspx?" + sKey);
                        }
                  });
            }
      }
}
